﻿import com.dinmax.isolution.*

class ISMain
implements DeviceListener, CommunicationListener {
	
	private static var app:ISMain;
	private var root:MovieClip;
	
	public static function getInstance(root:MovieClip):ISMain {
		if(app){ 
			return app;
		} else {
			app = new ISMain(root);
			return app;
		}
	};
	
	function ISMain(root:MovieClip){
		root ? this.root = root : this.root = _root;
		this.loadData();		
		return;
	}
	
	private function loadData():Void {
		//trace("ISMain loadData")
		//trace("ISMain dataLoaded")
		this.doCommunication();
		return;
	}
	
	private function doCommunication():Void {
		//trace("ISMain doCommunication")
		Communication.getInstance(this);
		return;
	}
	
	public function deviceChange(state:DeviceState):Void {
		//por ahora no está haciendo nada esto
		return;
	};
	
	public function communicationEvent(e:Array):Void {
		trace("ISMain | communicationEvent | new events:"+e.length);
		for (var i:Number=0;i<e.length;i++) {
			if (e[i] instanceof Command) {
				trace("event command!");
			} else if (e[i] instanceof Device) {
				trace("event device!");
				HCC.updateDevice(e[i]);	
				ISAppGUI.getActive().updateDevice(e[i]);
			} else if (e[i] instanceof Theme) {
				trace("event theme!");
				HCC.updateTheme(e[i]);
				ISAppGUI.getActive().updateTheme(e[i]);
			} else if (e[i] instanceof Person) {
				trace("event person!");
				ISAppGUI.getActive().showPerson(e[i]);
			}
		}
		trace("---");
		return;
	};
	
}